<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>报告管理</title>
    <style>
    </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>


<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:">首页</a><span lay-separator="">/</span>
        <a href="javascript:">报告管理</a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>


<div class="layui-fluid">
    <div class="layui-card">
        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
        <script type="text/html" id="toolbarDemo">
            <a class="layui-btn layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加报告</a>
        </script>
        <script type="text/html" id="table-useradmin-webuser">

            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>查看</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>
<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element" id="myform">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">报告名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" id="title" placeholder="请输入报告名称" lay-verify="required" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">报告者</label>
                <div class="layui-input-inline">
                    <input type="text" name="comiter" id="comiter" placeholder="请输入报告者" lay-verify="required" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>关键信息</legend>
        </fieldset>

        <div class="layui-card-body layui-table-body layui-table-main">
            <table class="layui-table layui-form" id="tables">
                <thead>
                <tr>
                    <th>标题</th>
                    <th>内容</th>
                    <th>
                        <button type="button" class="layui-btn" id="add" onsubmit='return false'>添加</button>
                    </th>

                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><input type="text" name="b" placeholder="请输入标题" lay-verify="required" autocomplete="off"
                               class="layui-input"></td>
                    <td><input type="text" name="n" placeholder="请输入内容" lay-verify="required" autocomplete="off"
                               class="layui-input"></td>
                    <td>
                        <button type="button" class="layui-btn" id="deletes" onsubmit='return false'>删除</button>
                    </td>
                </tr>
                </tbody>
            </table>

            <table class="layui-hide" id="demo" lay-filter="demo"></table>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>统计模板</legend>
        </fieldset>

        <div class="layui-form-item">

            <div class="layui-input-block" id="chok">


            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>问题信息</legend>
        </fieldset>
        <div class="layui-card-body layui-table-body layui-table-main">
            <table class="layui-table layui-form" id="tabless">
                <thead>
                <tr>
                    <th>标题</th>
                    <th>内容</th>
                    <th>文件</th>
                    <th>
                        <button type="button" class="layui-btn" id="adds" onsubmit='return false'>添加</button>
                    </th>

                </tr>
                </thead>
                <tbody>
                <tr>

                    <td><input type="text" name="t" placeholder="请输入标题" lay-verify="required" autocomplete="off"
                               class="layui-input"></td>
                    <td><input type="text" name="tc" placeholder="请输入内容" lay-verify="required" autocomplete="off"
                               class="layui-input"></td>
                    <td><input type="hidden" name="filenum"><input type="file" onchange="chare(this)" id="file"
                                                                 name="file" multiple="multiple" lay-verify="required"
                                                                 autocomplete="off" class="layui-input"></td>
                    <td>
                        <button type="button" class="layui-btn" id="deletess" onsubmit='return false'>删除</button>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" onclick="tj()" lay-submit="" lay-filter="component-form-element">立即提交</button>

            </div>
        </div>
    </form>
</div>


<%include("/common/foot.html"){}%>
<script>
    var a = 0;
    $("#add").click(function () {
        a++;
        $("#tables").append("<tr>\n" +
            "                        <td>  <input type=\"text\" id='b' name=\"b\" placeholder='请输入标题' lay-verify=\"required\" autocomplete=\"off\" class=\"layui-input bt\"></td>\n" +
            "                        <td >  <input type=\"text\" id='n' name=\"n\" placeholder='请输入内容' lay-verify=\"required\" autocomplete=\"off\" class=\"layui-input nr\"></td>\n" +
            "                        <td >  <button type=\"button\"  class=\"layui-btn ids\" >删除</button></td>\n" +
            "                    </tr>")

    });
    $(document).on('click', '.ids', function () {
        $(this).parent().parent().remove();
    });

    var c = 0;
    $("#adds").click(function () {
        c++;
        $("#tabless").append("<tr>\n" +
            "                        <td>  <input type=\"text\" id='t' name=\"t\" placeholder='请输入标题' lay-verify=\"required\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                        <td>  <input type=\"text\" id='tc' name=\"tc\" placeholder='请输入内容' lay-verify=\"required\" autocomplete=\"off\" class=\"layui-input\"></td>\n" +
            "                        <td > <input type=\"hidden\" name=\"filenum\"> <input type=\"file\" multiple=\"multiple\" onchange=\"chare(this)\" id='f' name=\"file\" lay-verify=\"required\" autocomplete=\"off\" class=\"layui-input wj\"></td>\n" +
            "                        <td >  <button type=\"button\"  class=\"layui-btn idss\" >删除</button></td>\n" +
            "                    </tr>")

    });
    $(document).on('click', '.idss', function () {
        $(this).parent().parent().remove();
    });


    $(function () {
        $.ajax({
            url: "temp/getAll.json",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            data: {},    //参数值
            type: "post",   //请求方式
            beforeSend: function () {
                //请求前的处理
            },
            success: function (req) {
                //请求成功时处理
                for (var i = 0; i < req.data.length; i++) {
                    $("#chok").html(" <input  type=\"checkbox\" value='" + req.data[i].id + "' name=\"interest\" title=\"" + req.data[i].title + "\" >");
                }
            },
            complete: function () {
                //请求完成的处理
            },
            error: function (error) {
                //请求出错处理
                alert(JSON.stringify(error))

            }
        });
    });


    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;

        var title = "报告信息";
        var findAll_url = 'tempmanage/list.json';
        var update_url = 'tempmanage/update.jhtml';
        var delete_url = 'tempmanage/delete.json';
        var index;//定义建立富文本编辑器全局变量
        var layedit;//定义富文本编辑器layedit全局变量
        var cols = [ //表头
            /*{checkbox: true, fixed: true},
            {
              field: 'createDate',
               
              templet: "<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd ')}}</div>",
              title: '发布时间',
              fixed: 'left',
            },*/


            {field: 'title', title: '标题'},
            {field: 'comiter', sort: 'true', title: '提交人'},
            {field: 'comitetime', templet:"<div>{{layui.util.toDateString(d.comitetime, 'yyyy-MM-dd HH:mm:ss')}}</div>",title: '创建时间'},
            {title: '操作', fixed: 'right', width: 215, toolbar: '#table-useradmin-webuser',}
        ];
        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );


        //初始化修改页面

        function loadData(data) {

            var ret = {
                "id": data.id,
                // "classify": data.classifys.name,
                "s_classify": data.s_classify,
                "goodsName": data.goodsName,
                "price": data.price,
                "introduce": data.introduce,
                "status": data.status,
                "time": data.time,
                "sex": data.sex,
                "age": data.age,
                "seniority": data.seniority,
                "year": data.year,
                "tel": data.tel,
                "address": data.address,
                "account": data.account,
                "charge": data.charge,
                "lng": data.lng,
                "lat": data.lat,
            };
            $("#img1").attr("src", data.img);
            $("#classifys").val(data.classifys.id);

            form.render();

        }


        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            var a = layer.open({
                type: 1,
                area:['800px','700px'],
                title: title,
                content: $('#layuiadmin-form-useradmin'),
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });
            // layer.full(a);
            //服务分类

            //富文本编辑器
            layedit = layui.layedit;
            //建立编辑器
            index = layedit.build('introduce', {
                tool: ['strong', 'italic', 'underline', 'del', '|',
                    'left', 'center', 'right', '|',
                    'link', 'unlink',],
                height: 280
            });

            //富文本表单验证
            form.verify({
                introduce: function (value) {
                    var val = layedit.getText(index);
                    //alert(val.length)
                    if (val.length < 6) {

                        return '内容至少6个字符'
                    }
                }
            });
            //如果是添加则重置表单，如果是编辑则给表单赋值
            if (data == 0 || data == null) {
                $('#myform')[0].reset();

            } else {
                layui.form.val("component-form-element", loadData(data));
            }


        }


        //弹出查看页面调用初始化数据方法
        function initUpdate1(data) {
            var a = layer.open({
                type: 1,
                title: title,
                content: $('#layuiadmin-form-useradmin1'),
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    //location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });
            layer.full(a);
            //给表单赋值
            $("#classify1").val(data.classifys.name);
            $("#goodsName1").val(data.goodsName);
            $("#price1").val(data.price);
            var introduce = data.introduce;
            introduce = introduce.replace(/(\n)/g, "");
            introduce = introduce.replace(/(\t)/g, "");
            introduce = introduce.replace(/(\r)/g, "");
            introduce = introduce.replace(/<\/?[^>]*>/g, "");
            introduce = introduce.replace(/\s*/g, "");
            $("#introduce1").val(introduce);
            $("#status1").val(data.status);
            $("#time1").val(data.time);
            $("#sex1").val(data.sex);
            $("#age1").val(data.age);

        }


        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function (data) {
            data.field.introduce = layedit.getContent(index);
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id; //得到复选框的值
                            postDelete(a);
                        }
                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
                    // alert(data.id);
                    postDelete(data.id);
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    area: [$(window).width()+'px', $(window).height() +'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: false,
                    shade:0.4,
                    title: title,
                    content: "tempmanage/viow/"+data.id+".jhtml"
                });
                // initUpdate(data);
            } else if (obj.event === 'detail') {
                initUpdate1(data);
            }
        });


        //ajax提交删除内容
        window.postDelete = function (id) {
            window.location.href = "tempmanage/delete.json?id="+id;
            // AjaxCommon.ajax(delete_url, "post", "json", {"id": id}, function (ret) {
            //     if (ret == null) {
            //         layer.msg("删除失败", {
            //             icon: 5
            //         });
            //     } else {
            //         layer.msg("删除成功", {
            //             icon: 6
            //         });
            //         layer.closeAll();
            //         table.reload('listReload');
            //     }
            //     return ret;
            // });
        };

        //ajax提交修改内容
        window.postUpdate = function (data) {
            var formData = new FormData($("#myform")[0]);

            var chk_value =[];//定义一个数组
            $('input[name="interest"]:checked').each(function(){//遍历每一个名字为interest的复选框，其中选中的执行函数
                chk_value.push($(this).val());//将选中的值添加到数组chk_value中
            });
            formData.append("chk_value", chk_value);
            // var comiter = $("#comiter").val();
            // var title =  $("#title").val();
            // formData.append("comiter",comiter);
            // formData.append("title",title);
            $.ajax({
                url: update_url,
                type: "POST",
                traditional: true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');
                            location.replace(location.href);
                        });
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

    function chare(t) {
        //alert(t.previousSibling.innerHTML)
        $(t).prev().val(t.files.length);
    }
</script>


</body>

</html>
